package sketch.specs;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;

import sketch.ast.ASTUtils;
import sketch.util.Checker;

public class RandomValueExtractor {

	public final CompilationUnit unit;
	
	public RandomValueExtractor(CompilationUnit unit) {
		Checker.checkNull(unit, "The compilation unit could not be null.");
		this.unit = unit;
	}
	
	public Map<MethodDeclaration, List<RandomValueInfo>> extract_random_values() {
		Map<MethodDeclaration, List<RandomValueInfo>> map = new LinkedHashMap<MethodDeclaration, List<RandomValueInfo>>();
		List<MethodDeclaration> methods = ASTUtils.getAllMethods(unit);
		
		for(MethodDeclaration method : methods) {
			RandomValueVisitor visitor = new RandomValueVisitor();
			method.accept(visitor);
			if(visitor.randomValues.isEmpty()) {
				continue;
			} else {
				map.put(method, new LinkedList<RandomValueInfo>());
				for(MethodInvocation invocation : visitor.randomValues.keySet()) {
					RandomValueInfo info = new RandomValueInfo(visitor.randomValues.get(invocation).toString(), invocation);
					//compute the classes
					
					List<Class<?>> class_list = RandomValueVisitor.getClasses(invocation);
					info.class_list.addAll(class_list);
					
					map.get(method).add(info);
				}
			}
		}
		
		return map;
	}
}